package com.learn.core.controller.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.learn.common.annotation.Log;
import com.learn.common.annotation.UserLoginToken;
import com.learn.common.enums.BusinessType;
import com.learn.common.enums.OperatorType;
import com.learn.common.enums.ResultEnum;
import com.learn.common.utils.JwtUtil;
import com.learn.common.utils.PageResult;
import com.learn.common.utils.ResultUtil;
import com.learn.common.utils.StringUtils;
import com.learn.common.utils.result.CommonResult;
import com.learn.core.domain.LCourseware;
import com.learn.core.domain.LCoursewareType;
import com.learn.core.domain.LOrganization;
import com.learn.core.dto.LCoursewareDTO;
import com.learn.core.service.ILCoursewareService;
import com.learn.core.service.ILOrganizationService;
import com.learn.framework.web.controller.WebController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @auther CodeGenerator
 * @create 2021-03-13 19:39:54
 * @describe 课件表前端控制器
 */
@Api("课件")
@RestController
@RequestMapping("/manager/courseware")
public class LCoursewareController extends WebController<LCourseware> {

    @Autowired
    private ILCoursewareService iLCoursewareService;
    @Autowired
    private ILOrganizationService ilOrganizationService;

    //列表
    @ApiOperation(value = "获取列表")
    @UserLoginToken
    @PostMapping("/list")
    public PageResult<LCourseware> list(@RequestBody LCoursewareDTO lCoursewareDTO){
        try {
            if(lCoursewareDTO==null){
                return new PageResult<LCourseware>(null, null, ResultEnum.ERROR);
            }
            if(lCoursewareDTO.getPage()==null||lCoursewareDTO.getRows()==null){
                return new PageResult<LCourseware>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lCoursewareDTO.getPage(),lCoursewareDTO.getRows());
            QueryWrapper<LCourseware> queryWrapper = new QueryWrapper<LCourseware>();
            if(lCoursewareDTO.getOrganizationId()!=null)
                queryWrapper.eq("organization_id",lCoursewareDTO.getOrganizationId());
            if(StringUtils.isNotEmpty(lCoursewareDTO.getTeacherName()))
                queryWrapper.eq("courseware_name",lCoursewareDTO.getCoursewareName());
            if(StringUtils.isNotEmpty(lCoursewareDTO.getStutes()))
                queryWrapper.eq("stutes",lCoursewareDTO.getStutes());
            if(StringUtils.isNotEmpty(lCoursewareDTO.getTeacherName()))
                queryWrapper.eq("teacher_name",lCoursewareDTO.getTeacherName());
            if(StringUtils.isNotEmpty(lCoursewareDTO.getCoursewareTypeId()))
                queryWrapper.eq("courseware_type_id",lCoursewareDTO.getCoursewareTypeId());
            if(lCoursewareDTO.getSortType()!=null&&lCoursewareDTO.getSortBy()!=null)
                queryWrapper.orderBy(true,lCoursewareDTO.getSortType(),lCoursewareDTO.getSortBy());
            Page<LCourseware> lCoursewares = (Page<LCourseware>) iLCoursewareService.list(queryWrapper);
            if (CollectionUtils.isEmpty(lCoursewares)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            for (LCourseware lCourseware: lCoursewares) {
                LOrganization lOrganization = ilOrganizationService.getById(lCourseware.getOrganizationId());
                if(null != lOrganization)
                    lCourseware.setOrganizationName(lOrganization.getOrganizationName());
            }
            // 返回
            return new PageResult(lCoursewares.getTotal(), lCoursewares, ResultEnum.SUCCESS);
        }catch (Exception e){
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }


    //新增
    @ApiOperation(value = "添加")
    @PostMapping("/add")
    @UserLoginToken
    @Log(title = "添加课件", businessType = BusinessType.INSERT, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> add(@RequestBody LCourseware lCourseware, HttpServletRequest request){
        // token
        String token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        try {
            if(lCourseware!=null) {
                if (StringUtils.isEmpty(lCourseware.getCoursewareNme())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_COURSEWARE_NAME);
                }
                if (StringUtils.isEmpty(lCourseware.getStutes())) {
                    lCourseware.setStutes("1");
                }
                if (StringUtils.isEmpty(lCourseware.getCoursewareType())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_COURSEWARE_TYPE);
                }
                if (StringUtils.isEmpty(lCourseware.getCoursewareHours())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_COURSEWARE_HOUR);
                }
                if (StringUtils.isEmpty(lCourseware.getOrganizationId())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_COURSEWARE_ORG);
                }
                if (StringUtils.isEmpty(lCourseware.getTeacherId())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_COURSEWARE_TEACHER);
                }
                if (StringUtils.isEmpty(lCourseware.getTeacherName())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_COURSEWARE_TNAME);
                }
                if (StringUtils.isEmpty(lCourseware.getCoursewareTypeId())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_COURSEWARE_TYPE_ID);
                }
                if (StringUtils.isEmpty(lCourseware.getResourcesLink())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_COURSEWARE_RESOURCES);
                }
                lCourseware.setCreateUser(userId);
                lCourseware.setCreateTime(new Date());
                if (iLCoursewareService.save(lCourseware)) {
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return  (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }


    //删除
    @ApiOperation(value = "删除")
    @PostMapping("/del")
    @UserLoginToken
    @Log(title = "删除课件", businessType = BusinessType.DELETE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> del(@RequestBody LCourseware lCourseware){
        try {
            if(lCourseware!=null){
                if(lCourseware.getCoursewareId()!=null) {
                    if(iLCoursewareService.removeById(lCourseware.getCoursewareId()))
                        return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e)
        {
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //更新
    @ApiOperation(value = "更新")
    @PostMapping("/edit")
    @UserLoginToken
    @Log(title = "更新课件", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> edit(@RequestBody LCourseware lCourseware, HttpServletRequest request) {
        // token
        String token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        if(lCourseware!=null){
            lCourseware.setUpdateTime(new Date());
            lCourseware.setUpdateUser(userId);
            if(iLCoursewareService.updateById(lCourseware)){
                return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
            }
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }
}

